package com.itheima.dubbo.mysql.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.domain.User;
import com.itheima.commons.pojo.entity.Log;
import com.itheima.dubbo.interfaces.KeepApi;
import com.itheima.dubbo.mysql.mapper.LoginLogMapper;
import com.itheima.dubbo.mysql.mapper.UserInfoPlusMapper;
import com.itheima.dubbo.mysql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class KeepApiImpl implements KeepApi {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoPlusMapper userInfoPlusMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;

    //通过时间查看注册的人数
    @Override
    public int getNumBydate(Date date, Date date2, String sex) {
        Integer integer = 0;
        System.out.println(date);
        System.out.println(date2);
        integer = userMapper.selectCount(Wrappers.lambdaQuery(User.class)
                .ge(User::getCreated, date).lt(User::getCreated, date2)
        );

        return integer;
    }

    //通过注册时间查询登录的人id的集合
    @Override
    public List<Long> getIdbydate(Date date, Date date2) {
        List<Log> logs = loginLogMapper.selectList(Wrappers.lambdaQuery(Log.class)
                .ge(Log::getLogTime, date).lt(Log::getLogTime, date2)
        );
        //获得id的集合
        List<Long> longs = new ArrayList<>();
        for (Log log : logs) {
            longs.add(log.getId());
        }
        return longs;
    }

    //通过注册时间查询注册id
    @Override
    public List<Long> getIdbycreattime(Date date, Date date2, String sex) {
        List<User> users = userMapper.selectList(Wrappers.lambdaQuery(User.class)
                .ge(User::getCreated, date).lt(User::getCreated, date2)
        );
        //获得id的集合
        List<Long> ids = new ArrayList<>();
        for (User user : users) {
            ids.add(user.getId());
        }
        return ids;
    }


    //通过时间查看登录的userid
    @Override
    public List<Long> getLogNum(Date date, Date date2, String sex) {
        List<Log> logs = loginLogMapper.selectList(Wrappers.lambdaQuery(Log.class)
                .ge(Log::getLogTime, date).lt(Log::getLogTime, date2)
        );
        List<Long> longs = new ArrayList<>();
        for (Log log : logs) {
            longs.add(log.getUserId());
        }

        return longs;
    }
}
